fix(bedrock): fall back to toolChoice.auto when model does not support toolChoice.any#2075
Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…t toolChoice.any The Bedrock Converse API only supports toolChoice.any for Anthropic Claude models. Other families (Meta Llama, Amazon Titan/Nova, Mistral, Cohere) reject it with ValidationException. When structured output enters forced mode it sends toolChoice.any, causing crashes on non-Claude Bedrock models. Add _resolve_tool_choice to BedrockModel that detects support via _MODELS_SUPPORT_TOOL_CHOICE_ANY and falls back to toolChoice.auto with a warning log for unsupported models. Follows the existing _MODELS_INCLUDE_STATUS pattern in the file. Fixes strands-agents#1241
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When the structured output context enters forced mode it sets
tool_choice = {"any": {}}torequire the model to call the structured output tool.
BedrockModel._format_requestforwardedthis directly as
toolConfig.toolChoice.anyto the Bedrock Converse API.The Bedrock Converse API only supports
toolChoice.anyfor Anthropic Claude models. All othermodel families — Meta Llama, Amazon Titan/Nova, Mistral, Cohere — reject it with a
ValidationException: This model doesn't support the toolConfig.toolChoice.any field.The fix adds a
_resolve_tool_choicehelper toBedrockModelthat detects whether the activemodel supports
toolChoice.anyand falls back totoolChoice.autowhen it does not. The modellist follows the existing
_MODELS_INCLUDE_STATUSpattern already used in the file.Related Issues
Fixes #1241
Documentation PR
N/A
Type of Change
Bug fix
Testing
How have you tested the change?
hatch run preparetest_format_request_tool_choice_anyusing a Claude model ID to verifytoolChoice.anypasses through unchangedtest_format_request_tool_choice_any_falls_back_for_unsupported_modelsusing a non-Claude model ID to verify fallback totoolChoice.autoChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.